<?php
namespace app\work\logic;
use app\work\validate\WorkRecordData as WorkRecordDataValidate;

class DataLogic extends BaseLogic {

    /**
     * 检查新增每日数据规则
     * @param $department
     * @param $params
     * @return mixed
     */
    public function check_add_data_rule($department,$params){
        $validate = new WorkRecordDataValidate();
        $r = $validate->scene('add')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        if(empty($department)){
            return $this->get_response(0,'您没有部门，不能提交');
        }

        return $this->get_response(1,'OK');
    }

    /**
     * 同步发送工作圈
     * @param $openid
     * @param $department
     * @param $params
     * @return mixed
     */
    public function sync_send_wcm($openid,$department,$params){
        $content = '我刚刚提交了今天【' . $department . '】的【每日数据】';

        $media = $pic_arr_new = [];
//        $pic_arr = $params['pic'];
//        if(!empty($pic_arr)){
//            array_map(function ($value,$key) use (&$pic_arr_new){
//                $pic_arr_new[$key]['pic_url'] = $value;
//            },$pic_arr,array_keys($pic_arr));
//
//            $media['data'] = $pic_arr_new;
//            $media['type'] = 'pic';
//        }

        $wcm_data = [];
        $wcm_data['openid'] = $openid;
        $wcm_data['content'] = $content;
        $wcm_data['media'] = json_encode($media,JSON_UNESCAPED_UNICODE);
        $r = model('work_circle_msg')->insert($wcm_data);
        if($r){
            return $this->get_response(1,'同步成功');
        }
        return $this->get_response(0,'同步失败');
    }
}